package fy.form.groovy;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.bo.V3xOrgPost;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.v3x.services.V3XLocator;

public class GroovyFunc {

    private static final Log logger = LogFactory.getLog(GroovyFunc.class);
    // private static final int CHECK_IS_AUDIT = 0;// 查看人员是否审批人员
    // private static final int CHECK_SHENHECISHU = 1;// 非审批人员需要审批几次
/*    private static final long shenpi1 = 2598272894397269345L;
    private static final long shenpi2 = 2598242906183847131L;
    private static final long shenpi3 = 1325558360490659349L;
    private static final long shenpi4 = 5502099106103904405L;*/
/*    61006780778215055152273590538694509274-3827510971753903033842409365363314226
    084655福耀汽玻795676638130092599226230060922449604571862381521595417095-8947711667929330759
    084655福耀浮法61006780778215055152273590538694509274-3827510971753903033842409365363314226
    084474福耀玻璃工业集团股份有限公司（总办）61006780778215055152273590538694509274-3827510971753903033842409365363314226
    084655福耀玻璃工业集团股份有限公司（总办）61006780778215055152273590538694509274-3827510971753903033842409365363314226*/
    private static long shenpi1 = 2598272894397269345L;
    private static long shenpi2 = 2598242906183847131L;
    private static long shenpi3 = 1325558360490659349L;
    private static long shenpi4 = 5502099106103904405L;
    //查詢方法
    private static int check(long deptID, long postid, long userID, int result,
            OrgManager orgManager) throws Exception {
    	
        List<V3xOrgMember> members = orgManager.getMembersByDepartmentPost(
                deptID, postid);//根据部門ID，崗位ID列舉人員     
        
//        logger.info("111:"+deptID+","+postid+","+userID+".");
//        logger.info("000:"+members.size());
        if (members.size() > 0) {//人員存在      	
            for (int i = 0, size = members.size(); i < size; i++) {
//            	logger.info("+__+_:"+members.get(i).getId());
                if (userID == members.get(i).getId()) {//獲取人員ID=userID
                    return result - 1;//判斷從零開始人數減1
                }
            }
            return result;
        }
        return -2;
    }

    private static boolean post(long deptID, long postID, long userID,
            OrgManager orgManager) throws Exception {
        List<V3xOrgMember> members = orgManager.getMembersByDepartmentPost(
                deptID, postID);
        if (members.size() > 0) {
            for (int i = 0, size = members.size(); i < size; i++) {
                if (userID == members.get(i).getId()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取需审批次数
     *
     * @param code
     * @param loginAccountName
     * @return
     */
    public static int getAuditCount(Object object) {
        // System.out.println(object);
        // logger.info(object);
        // String loginName, String loginAccountName
        // return fy.form.groovy.GroovyFunc.getAuditCount(param);
        Object[] array = (Object[]) object;

        if (("福耀玻璃工业集团股份有限公司（总办）FYG".equals(array[1].toString().trim())) || ("福耀ARG".equals(array[1].toString().trim())) || 
        	      ("福耀浮法FYF".equals(array[1].toString().trim())) || ("福耀汽玻FYA".equals(array[1].toString().trim())) || 
        	      ("福建三锋汽车服务有限公司".equals(array[1].toString().trim())) || ("福耀模具公司".equals(array[1].toString().trim())) || 
        	      ("福耀饰件公司".equals(array[1].toString().trim())) || ("福耀汽车铝件（福建）有限公司".equals(array[1].toString().trim())) || ("福耀模具厦门厂".equals(array[1].toString().trim())) || 
        	      ("福耀模具福州厂".equals(array[1].toString().trim()))) {
        	      try
        	      {
                  	String loginName=AppContext.getCurrentUser().getLoginName();
                  	logger.info("名"+loginName);
                    String loginAccountName =  array[1].toString().trim();
        	        if ("福耀汽玻FYA".equals(loginAccountName))
        	        {
        	          shenpi1 = 7956766381300925992L;
        	          shenpi2 = 2623006092244960457L;
        	          shenpi3 = 1862381521595417095L;
        	          shenpi4 = -8947711667929330759L;
        	        }
        	        else if ("福耀ARG".equals(loginAccountName))
        	        {
        	          shenpi1 = -7529433238598629885L;
        	          shenpi2 = 1944221980327955997L;
        	          shenpi3 = -6145420888250891774L;
        	          shenpi4 = -2185706993032672511L;
        	        }
        	        else if ("福耀浮法FYF".equals(loginAccountName))
        	        {
        	          shenpi1 = 6100678077821505515L;
        	          shenpi2 = 2273590538694509274L;
        	          shenpi3 = -382751097175390303L;
        	          shenpi4 = 3842409365363314226L;
        	        }
        	        else if ("福建三锋汽车服务有限公司".equals(loginAccountName))
        	        {
        	          shenpi1 = -7435494521112673762L;
        	          shenpi2 = 4495447923143821052L;
        	          shenpi3 = -7897170005669099777L;
        	          shenpi4 = -3631151797923002698L;
        	        }
        	        else if ("福耀模具公司".equals(loginAccountName))
        	        {
        	          shenpi1 = 8808066242158348667L;
        	          shenpi2 = 2284534947635807962L;
        	          shenpi3 = -1543748340332607632L;
        	          shenpi4 = -6724847303266224324L;
        	        }
        	        else if ("福耀饰件公司".equals(loginAccountName))
        	        {
        	          shenpi1 = -3114502697023877794L;
        	          shenpi2 = -3379485291226117506L;
        	          shenpi3 = 8000774662389188930L;
        	          shenpi4 = 1954137031617430262L;
        	        }
        	        else if ("福耀汽车铝件（福建）有限公司".equals(loginAccountName))
        	        {
        	          shenpi1 = 7286755558687197339L;
        	          shenpi2 = 6601008492201332510L;
        	          shenpi3 = -2642400943492676065L;
        	          shenpi4 = 915968762101846012L;
        	        }
        	        else if ("福耀模具厦门厂".equals(loginAccountName))
        	        {
        	          shenpi1 = 7017157418095824560L;
        	          shenpi2 = 4293984431150549065L;
        	          shenpi3 = -6616615144033138273L;
        	          shenpi4 = -4846195302009453083L;
        	        }
        	        else if ("福耀模具福州厂".equals(loginAccountName))
        	        {
        	          shenpi1 = -4995703792973494498L;
        	          shenpi2 = -4180479752519070835L;
        	          shenpi3 = 9038767918843513359L;
        	          shenpi4 = -5331833372917610695L;
        	        }
        	        else
        	        {
        	          shenpi1 = 2598272894397269345L;
        	          shenpi2 = 2598242906183847131L;
        	          shenpi3 = 1325558360490659349L;
        	          shenpi4 = 5502099106103904405L;
        	        }
        	        logger.info(loginAccountName);
              
              //6100678077821505515    2273590538694509274   -382751097175390303    3842409365363314226
              //6100678077821505515    2273590538694509274    -382751097175390303   3842409365363314226
                logger.info(loginName+loginAccountName+shenpi1+shenpi2+shenpi3+shenpi4);
                OrgManager orgManager = V3XLocator.getInstance().lookup(
                        OrgManager.class);
                List depList = new ArrayList();
                V3xOrgMember member = orgManager
                        .getMemberByLoginName(loginName);
                long deptId = member.getOrgDepartmentId();
                      logger.info("deptId = "+deptId);
                depList = orgManager.getUserDomainIDs(
                        Long.valueOf(member.getId()),
                        Long.valueOf(orgManager.getAccountByName(
                                        loginAccountName).getId()),
                        new String[]{"Department"});
//                if (depList.size() > 0) {
//                    V3xOrgDepartment dept = (V3xOrgDepartment) depList.get(0);
//                    if (dept != null) {
//                        deptId = (dept.getId());
//                    }
//                }
//                if (depList.size() > 0) {
//                    try {
//                    	
//                    	String dd= depList.get(depList.size()-1).toString();
//						if(dd!=null){
//							
//							deptId = Long.parseLong( depList.get(0).toString());
//						}
//                    			
//                    		
//						
//						
//					} catch (Exception e) {
//						logger.info("获取部门id异常");
//					}
//                }
                List<V3xOrgPost> posts = orgManager.getDepartmentPost(deptId);
                boolean exist = false;
                for (int i = 0, size = posts.size(); i < size; i++) {
                    long postID = posts.get(i).getId();
                    logger.info("postID = "+postID);
                    if (shenpi4 == postID || shenpi3 == postID
                            || shenpi2 == postID || shenpi1 == postID) {
                        exist = true;
                    }
                }
                if (!exist) {
                    V3xOrgDepartment pd = orgManager.getParentDepartment(deptId);
                    if (pd != null) {
                        deptId = pd.getId();
                    }
                }
//                deptId=4594242975542103074l;
                long userID = member.getId();
                int result = 0;
                if ((result = check(deptId, shenpi4, userID, 4, orgManager)) != -2) {
                    if (post(deptId, shenpi1, userID, orgManager)) {
                        result = 0;
                    } else if (post(deptId, shenpi2, userID, orgManager)) {
//                    	logger.info("123"+userID);
                        result = 1;
                    } else if (post(deptId, shenpi3, userID, orgManager)) {
                        result = 2;
                    }
                } else if ((result = check(deptId, shenpi3, userID, 3,
                        orgManager)) != -2) {
                    if (post(deptId, shenpi1, userID, orgManager)) {
                        result = 0;
                    } else if (post(deptId, shenpi2, userID, orgManager)) {
//                    	logger.info("321"+userID);
                        result = 1;
                    }
                } else if ((result = check(deptId, shenpi2, userID, 2,
                        orgManager)) != -2) {
                	
                    if (post(deptId, shenpi1, userID, orgManager)) {
                        result = 0;
                    }
                    
                } else if ((result = check(deptId, shenpi1, userID, 1,
                        orgManager)) != -2) {
                	
                }
                return (result);
            } catch (Exception e) {
                logger.error("需审批次数获取异常", e);
            }
        }
        return -1;
    }

    // public static int getAuditCount1(Object object) {
    // return getAuditCount(object);
    // }
    // public static String getObjectStyle(Object object) {
    // Object[] array = (Object[]) object;
    //
    // System.out.println(array[0]);
    // logger.info(array[0]);
    // System.out.println(array[1]);
    // logger.info(array[1]);
    // return "12";
    // }
    public static int getFengGuan(Object object) {
        Object[] array = (Object[]) object;
        if ("福耀玻璃工业集团股份有限公司（总办）FYG".equals(array[1].toString().trim())) {
            try {
                //String loginName =  array[0].toString().trim();
            	String loginName=AppContext.getCurrentUser().getLoginName();
                String loginAccountName = array[1].toString().trim();
                OrgManager orgManager = V3XLocator.getInstance().lookup(
                        OrgManager.class);
                List depList = new ArrayList();
                V3xOrgMember member = orgManager
                        .getMemberByLoginName(loginName);
                long deptId = member.getOrgDepartmentId();
                depList = orgManager.getUserDomainIDs(
                        Long.valueOf(member.getId()),
                        Long.valueOf(orgManager.getAccountByName(
                                        loginAccountName).getId()),
                        new String[]{"Department"});
                if (depList.size() > 0) {
//                    V3xOrgDepartment dept = (V3xOrgDepartment) depList.get(0);
//                	if (dept != null) {
//                		deptId = (dept.getId());
//                	}
                	try{
	                	long dept = Long.parseLong(depList.get(0).toString());
	                	deptId = dept;
                	}
                	catch(NumberFormatException e){
                	}
                }
                long postID = -803245032428191421L;// 分管岗位ID
                if (orgManager.getMembersByDepartmentPost(deptId, postID)
                        .size() > 0) {
                    return 1;
                }
                String parentPath = orgManager.getDepartmentById(deptId)
                        .getParentPath();
                V3xOrgDepartment parentDept = null;

                for (int i = 0; i < 5; i++) {
                    if (parentPath == null || "".equals(parentPath)
                            || "0".equals(parentPath)) {
                        break;
                    }

                    parentDept = orgManager.getDepartmentByPath(parentPath);
                    if (parentDept == null) {
                        break;
                    }

                    if (orgManager.getMembersByDepartmentPost(
                            parentDept.getId(), postID).size() > 0) {
                        return 1;
                    }
                    if ("0000000103000020".equals(parentPath)) {
                        break;
                    }
                    parentPath = parentDept.getParentPath();

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    // ///////////////////
    private static long[] deptArray = {2869001411135357909L,// 基建
        -3602358740144142925L,// 信息部
        7815628411628440028L,// 行政
        2772581170730454430L,// 配件
        6258262779454245335L,// 人事
        87988775416582016L, // 质管
        //1097534762777136334L,//宣传部
        -7370705342937462L//供应管理部
    //			487067892445627598L // 研究院
    };

    private static boolean checkZongcai(long deptID) {
        for (int i = 0, size = deptArray.length; i < size; i++) {
            if (deptArray[i] == deptID) {
                return true;
            }
        }
        return false;
    }

    public static int zongcaiaudit(Object object) {
        // return fy.form.groovy.GroovyFunc.getAuditCount(param);
        // return fy.form.groovy.GroovyFunc.getFengGuan(param);
        // return fy.form.groovy.GroovyFunc.zongcaiaudit(param);

        Object[] array = (Object[]) object;
        if ("福耀玻璃工业集团股份有限公司（总办）FYG".equals(array[1].toString().trim())) {
            try {
                String deptName =  array[0].toString().trim();
                String loginAccountName =  array[1].toString().trim();
                OrgManager orgManager = V3XLocator.getInstance().lookup(
                        OrgManager.class);
                long accountId = orgManager.getAccountByName(loginAccountName)
                        .getId();
                List<V3xOrgDepartment> list = orgManager.getDepartmentsByName(
                        deptName, accountId);
                long deptID = list.get(0).getId();
                V3xOrgDepartment dept = null;
                for (int i = 0; i < 4; i++) {
                    if (checkZongcai(deptID)) {
                        return 1;
                    } else {
                        dept = orgManager.getParentDepartment(deptID);
                        if (dept != null) {
                            deptID = dept.getId();
                            dept = null;
                        } else {
                            dept = null;
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
}
